//加载动画
var fav = false;
function Loading_animation() {
    //当页面加载的时候开始执行
    document.getElementById("top").style.display = "none";
        if(document.readyState == "complete" && fav == true) {
            var load = document.getElementById("tou");
            var x = load.style.display = "none";
            document.getElementById("top").style.display = "block";
            console.log(x);
        }
}

//鼠标移入事件，改变购物车的样式
function Shopping_cart_changes1() {
    var top2 = document.getElementById("top2");
    top2.style.backgroundColor="#fffefe";
    var top2_img = document.getElementById("top2_img");
    top2_img.setAttribute("src","img/home/shopping2.png");
    var  a = document.getElementById("top2_a");
    a.style.color = "orange";
}

function Shopping_cart_changes2() {
    var top2 = document.getElementById("top2");
    top2.style.backgroundColor="#424242";
    var top2_img = document.getElementById("top2_img");
    top2_img.setAttribute("src","img/home/shopping1.png");
    var  a = document.getElementById("top2_a");
    a.style.color = "#b0b0b0";
}

//菜单栏a标签的鼠标悬浮和离开事件，主要是实现a标签的交互效果
function topbar_text1(id) {
    var name = document.getElementById(id);
    name.style.color = "#b0b0b0";
}
function topbar_text2(id) {
    var name = document.getElementById(id);
    name.style.color = "white";
}

//标题a标签的鼠标悬浮和离开事件，主要是实现a标签的交互效果
function header_text1(id) {
    var name = document.getElementById(id);
    name.style.color = "black";
}

function header_text2(id) {
    var name = document.getElementById(id);
    name.style.color = "red";
}

//搜索按钮的鼠标悬浮和离开事件
function search_button1(id) {
    var search_id = document.getElementById(id);
    var search_img = document.getElementById("search_img");
    search_img.setAttribute("src","img/home/search1.png");
    search_id.style.backgroundColor = "#ffffff";
}

function search_button2(id) {
    var search_id = document.getElementById(id);
    var search_img = document.getElementById("search_img");
    search_img.setAttribute("src","img/home/search2.png");
    search_id.style.backgroundColor = "#ff6700";
}

//广告栏两侧的箭头
function home_button1(id) {
    var button_id = document.getElementById(id);
    button_id.style.backgroundColor = "";
    if (id == "home_left_button"){
        button_id.setAttribute("src","img/home/left.png");
    }else {
        button_id.setAttribute("src","img/home/right.png");
    }
}

function home_button2(id) {
    var button_id = document.getElementById(id);
    button_id.style.backgroundColor = "#515050";
    if (id == "home_left_button"){
        button_id.setAttribute("src","img/home/left_1.png");
    }else {
        button_id.setAttribute("src","img/home/right_1.png");
    }
}

//两侧箭头的点击事件用来切换广告
function button_home(id) {
    event.stopPropagation();
    var advertising = document.getElementById("advertising");
    var img = advertising.style.backgroundImage;
    if (id == "home_left_button"){
        if (img == 'url("img/home/advertising1.jpg")'){
            advertising.style.backgroundImage = 'url("img/home/advertising2.jpg")';
            advertising.removeAttribute("onclick");
        }else {
            advertising.style.backgroundImage = 'url("img/home/advertising1.jpg")';
            advertising.setAttribute("onclick","Advertising_jump()");
        }
    }else {
        if (img == 'url("img/home/advertising2.jpg")'){
            advertising.style.backgroundImage = 'url("img/home/advertising1.jpg")';
            advertising.setAttribute("onclick","Advertising_jump()");
        }else {
            advertising.style.backgroundImage = 'url("img/home/advertising2.jpg")';
            advertising.removeAttribute("onclick");
        }
    }
}
//主页的广告切换和登录的判断
function start_timer(){
    login();
    Time();
}

//展示下面的商品的信息方便用户购买
function box_table_add() {
    var countl = 0;
    $(function () {
        $.ajax({
            url: "consigneeBypicture",
            type: "get",
            dataType: "json",
            success: function (res) {
                    var box_table = $("#box_table");
                    box_table.empty();
                    for (var j = 1; j <= Math.ceil(res.length / 4) ; j++) {
                        var tr = document.createElement("tr");
                        for (var i = countl; i < j * 4; i++) {
                            if (i<res.length) {
                                var td_1 = document.createElement("td");
                                td_1.setAttribute("align", "center");
                                td_1.setAttribute("onmouseover", "picturecolor(id)");
                                td_1.setAttribute("onmouseout", "picturedefaultcolor(id)")
                                td_1.classList.add("picture");
                                td_1.id = "picture" + i;

                                var a = document.createElement("a");
                                a.setAttribute("href", "/MyXiaomiShop/goodsDetail?consignee_id=" + res[i].id);
                                a.setAttribute("name", res[i].name);

                                var img_1 = document.createElement("img");
                                img_1.setAttribute("src", res[i].picture);

                                var br_1 = document.createElement("br");

                                var a_1 = document.createElement("a");
                                a_1.setAttribute("href", "/MyXiaomiShop/goodsDetail?consignee_id=" + res[i].id);
                                a_1.setAttribute("name", res[i].name);
                                a_1.innerText = res[i].name;

                                var br_2 = document.createElement("br");

                                var span = document.createElement("span");
                                span.innerText = res[i].price + "元";
                                span.style.color = "red";

                                a.appendChild(img_1);
                                td_1.appendChild(a);
                                td_1.appendChild(br_1);
                                td_1.appendChild(a_1);
                                td_1.appendChild(br_2);
                                td_1.appendChild(span);
                                tr.appendChild(td_1);
                                box_table.append(tr);
                            }
                        }
                        countl = i;
                    }
                fav=true;
                Loading_animation();
            },
            error: function (res) {
                alert("所有商品图片调用失败！")
            }
        });
    });
}
var time;
function stoptime() {
    var advertising = document.getElementById("advertising");
    var img = advertising.style.backgroundImage;
    if (img == 'url("img/home/advertising1.jpg")'){
        advertising.style.backgroundImage = 'url("img/home/advertising2.jpg")';
        advertising.removeAttribute("onclick");
    }else {
        advertising.style.backgroundImage = 'url("img/home/advertising1.jpg")';
        advertising.setAttribute("onclick","Advertising_jump()");
    }
    time=setTimeout("stoptime()",6000);
}
function Time() {
    time = setTimeout("stoptime()",10000);
}
//判断是否登录的函数
function login() {
    var user = document.getElementById("top1_login").innerText;
    var top1 = document.getElementById("top1");
    var container_top = document.getElementById("container_top");
    if (user.trim().length == 0){
        document.getElementById("top1_login").innerText = "登录";
        document.getElementById("top2_gwc").innerText = "0";
        top1.style.width = "140px";
        container_top.style.width = "240px";
        container_top.style.marginLeft = "288px";
    }else if (user.trim().length != 0 && user != "登录"){
        document.getElementById("top1_login").setAttribute("href","#");
        top1.style.width = "195px";
        container_top.style.width = "300px";
        container_top.style.marginLeft = "234px";
    }
}

//商品图片鼠标悬浮改变字体颜色为红色
function picturecolor(id) {
    var td = document.getElementById(id);
    for (var i = 0; i < td.children.length; i++) {
        if (td.childNodes[i].localName == "a"){
            td.childNodes[i].style.color = "red";
        }
    }
}
//商品图片鼠标悬浮改变字体颜色为默认颜色
function picturedefaultcolor(id) {
    var td = document.getElementById(id);
    for (var i = 0; i < td.children.length; i++) {
        if (td.childNodes[i].localName == "a"){
            td.childNodes[i].style.color = "#b0b0b0";
        }
    }
}

//搜索商品 并跳转到搜索页面
function search_product() {
    var product_name = document.getElementById("search_txt").value;
    if (product_name.trim().length == 0){
        box_table_add();
        return;
    }else {
        window.location.href = "/MyXiaomiShop/searchServlet?search_name="+product_name;
    }
}

function MI() {
    window.location.href = "/MyXiaomiShop/searchServlet";
}

function untapped() {
    alert("当前功能未开发，敬请期待");
}

//购物车的点击事件
function shopping_click() {
    var register =  document.getElementById("top2_a").getAttribute("name");
    if (register.trim().length==0){
        window.location.href = "/MyXiaomiShop/login.jsp";
    }else {
        window.location.href = "/MyXiaomiShop/cart.jsp";
    }
}

//我的订单点击事件
 function my_order() {
     var top1_login = document.getElementById("top1_login").innerText;
     if (top1_login == "登录"){
         window.location.href = "/MyXiaomiShop/login.jsp";
     }else {
         window.location.href = "/MyXiaomiShop/orderList.jsp";
     }
 }

 //广告点击事件
function Advertising_jump() {
    event.stopPropagation();
    window.location.href = "/MyXiaomiShop/goodsDetail?consignee_id=4";
}

